<?php

// Controller that outputs json instead of rendering views for REST purposes

class Api extends Controller {
    
    /* output functions */
    
    /**
     * Normal json output
     *
     * @param string $output objects to be encoded into json
     */
    public function output($output){
        header("Content-type: application/json");
        echo json_encode($output);
    }

    
    /* generic functions */
    
    public function get(){
        $model = ucwords($this->params['model']);
        if($this->params['id']){
            $obj = $this->db->getObject($model,$this->params['id']);
            $this->output($obj);
        } else {
            $list = $this->db->getList($model);
            $this->output($list);
        }
    }
    
    public function insert(){
        $model = ucwords($this->params['model']);
        if($model=="User"){
            
            // this is because the User model is the only one that requires processing of the password
            $this->createUser();
            
        } else {

            $object = modelAttribute($model);
            $msg = $this->db->save($object);
            if($msg!="ok"){
                $this->output($msg);
            } else {
                $this->output($object);
            }
        }
    }
    
    public function update(){
        $model = ucwords($this->params['model']);
        $id = $this->params['id'];
        $object = $this->db->getObject($model,$id);
        foreach(array_keys($this->params) as $field){
            if(in_array($field, $object->fields)){
                if($field=="password"){
                    $object->password = md5($this->params['password']);
                } else {
                    $object->$field = $this->params[$field];
                }
            } 
        }
        $msg = $this->db->update($object);
        if($msg!="ok"){
            $this->output($msg);
        } else {
            $this->output($object);
        }
    }
    
    public function delete(){
        $model = ucwords($this->params['model']);
        $id = $this->params['id'];
        $object = $this->db->getObject($model,$id);
        $this->db->delete($object);
        $obj = $this->db->getObject($model,$id);
        if($obj==null){
            $this->output("ok");
        } else {
            $this->output("delete failed");
        }
    }
    
    
    /* User functions */
    
    public function login(){
       // some login logic
    }
    
    // examples of CRUD...	
    public function createUser(){
        
        $user = new User();
        $user->fullname = $this->params['fullname'];
        $user->mobile = $this->params['mobile'];;
        $user->email = $this->params['email'];
        $user->password = md5($this->params['password']);
        $msg = $this->db->save($user);
        if($msg=="ok"){
            $this->output($user);
        } else {
            $this->output($msg);
        }    
    }
    
    public function getUser(){
        $user = $this->db->getObject("User", $this->params['id']);
        $this->output($user);
    }
    
    public function getUsers(){
        $users = $this->db->getList("User",($this->params['page']*15 - 15),$this->params['rp']);
        $this->output($users);
    }
    
    public function getUserByMobile(){
        $user = $this->db->getObject("User",$this->params['mobile'],"mobile");
        $this->output($user);
    }

    
    
    
    
}
